package patternRecognition;

import java.io.File;
import java.util.ArrayList;

import main.Main;

import recordProcessing.RecordParser;

import fields.FieldMatcher;
import fileProcessing.ParsingException;

public class QueryForHorizontalRecords extends PatternQuery implements Runnable{
	
	public QueryForHorizontalRecords(File f){
		super(f);
	}
	
	/**
	 * Determine fields and data by attempting to identify repeated words appearing in similar locations
	 * from line to line.
	 * @param file
	 * @throws ParsingException 
	 */
	public void performAnalysis() throws ParsingException{
		analyzeFile();
		
		int count,rowNumber=2;
		int mostMatches=0;
		ArrayList<String> currRowMatches = new ArrayList<String>();
		ArrayList<String> matchedFields = new ArrayList<String>();
		ArrayList<String> fieldData = new ArrayList<String>();
		
		//check each line to see if it contains field names
		for(ArrayList<String> row : wordsByRow){
			count=0;
			currRowMatches.clear();
			for(String s : row){//count the number of fields found per line of text.
				if(FieldMatcher.matchesField(s)){
					count++;//current word is a verified field name increment count
					currRowMatches.add(s);//then add the field name matched to current row matches array
				}
			}
			if(count>1){//if there are more than one field name match in the current row 
				int numberOfMatches=0,wCount=0,fCount=0;
				String dataHolder = "";
				ArrayList<String> tempData = new ArrayList<String>();//temporary storage for lines identified as records
				rowNumber=2;//skip the source and date lines
				for(ArrayList<String> line : wordsByRow){//iterate through all of the document lines
					rowNumber++;
					wCount=0;
					fCount=0;
					if((currRowMatches.size()>0) && line.containsAll(currRowMatches)){//if line contains the same fields as the current row 
						numberOfMatches++;
						dataHolder = "";
						for(String s : line){
							wCount++;
							if(wCount == line.size()){//if at the end of a line and found a keyword(no data after)
								if(currRowMatches.contains(s)){
									if(dataHolder.length()>0)
										tempData.add(dataHolder.substring(0, (dataHolder.length()-1)));
									else
										tempData.add("No_Value_Found");
									tempData.add("No_Value_Found");
								}
								else{
									tempData.add((s));
								}
							}
							else if(!currRowMatches.contains(s)){
								dataHolder += (s+" ");
							}
							else{//if keyword, store previous data values
								fCount++;
								if(fCount>1){
									if(dataHolder.length()==0){
										tempData.add("No_Value_Found");
									}
									else{
										try{
											tempData.add(dataHolder.substring(0, (dataHolder.length()-1)));
										} catch(Exception e){
											System.out.println(e.getMessage()+"Problem word: "+dataHolder);
										}
									}
								}
							}
						}
					}
				}
				if(numberOfMatches>mostMatches){//add only the list of 
					mostMatches=numberOfMatches;
					matchedFields.clear();
					matchedFields.addAll(currRowMatches);
					fieldData.clear();
					fieldData.addAll(tempData);
				}
			}
		}
		
		if(matchedFields.size()>0 && fieldData.size()>0){
			fields.addAll(matchedFields);
			data.addAll(fieldData);
		}
	}

	@Override
	public void run() {
		try {
			performAnalysis();
			RecordParser.threadCount++;
		} catch (ParsingException e) {
			try {
				Main.audit.writeToAudit(e.getMessage());
			} catch (ParsingException e1) {
			}
		}
	}
}
